﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace A1
{
    class PowerupBlock
    {
        private Rectangle rec; //The bounding rectangle
        private Texture2D spriteSheet; //The spriteSheet containing the sprite
        private int GameHeight; // The game's height
        private int GameWidth;//The Games width
        private Ball ball; // The ball
        private Random rand; // A random number generator
        private PowerUp.Powers power; //The power tihs block will hold
        private Rectangle textureRec; //The source rectangle for the texture
        private TimeSpan appearSpan; //The length of time for the block to appear
        private TimeSpan previousTime; //The previous time the block appeared
        private bool blockVisible; //Whether this block is visible

        /// <summary>
        /// The bounding rectangle
        /// </summary>
        public Rectangle Rec
        {
            get { return rec; }
        }

        /// <summary>
        /// The sprite of the powerblock
        /// </summary>
        public Texture2D Sprite
        { get { return spriteSheet; } }

        /// <summary>
        /// Create a new power block
        /// </summary>
        /// <param name="s">The texture</param>
        /// <param name="gh">The game height</param>
        /// <param name="gw">The game width</param>
        /// <param name="b">The ball</param>
        public PowerupBlock(Texture2D s, int gh, int gw, Ball b)
        {
            blockVisible = false;
            appearSpan = TimeSpan.FromSeconds(8);
            previousTime = TimeSpan.Zero;
            rec = new Rectangle(-500, -500, 50, 50); GameHeight = gh; GameWidth = gw; ball = b;
            spriteSheet = s;
            rand = new Random();
            textureRec = new Rectangle(0, 0, 50, 50);

        }

        /// <summary>
        /// Determines collision detecting with the ball
        /// </summary>
        /// <returns></returns>
        public PowerUp CollisionDetect()
        {
            int randomPower = rand.Next(0, 10);

            if (rec.Intersects(ball.Rec) && randomPower > 6)
            {
                ball.SpeedX *= -1; ball.SpeedY *= -1;
                power = (PowerUp.Powers)rand.Next(1, 3);
                PowerUp powerUp = new PowerUp(ball.LastHit, power, GameWidth, GameHeight, spriteSheet, rec);
                rec.Y = -500; rec.X = -500;

                return powerUp;
            }
            else if (rec.Intersects(ball.Rec) && randomPower <= 6)
            {
                int speedx = (int)ball.SpeedX;
                ball.SpeedX = ball.SpeedY;
                rec.Y = -500; rec.X = -500;
                ball.SpeedY = speedx;
                return null;
            }
            return null;
        }

        /// <summary>
        /// Causes the block to appear randomly
        /// </summary>
        /// <param name="gameTime">The gametime object</param>
        public void AppearRandomly(GameTime gameTime)
        {
            if (!blockVisible)
            {
                if (rand.Next(0, 101) == 100 && rec.Y == -500 && rec.X == -500)
                {
                    rec.Y = rand.Next(100, GameHeight - 100);
                    rec.X = rand.Next(100, GameWidth - 100);
                    previousTime = gameTime.TotalGameTime;
                    blockVisible = true;
                }
            }
            else
            {
                if (gameTime.TotalGameTime - previousTime > appearSpan)
                {
                    rec.Y = -500; rec.X = -500;
                    blockVisible = false;
                }
            }
        }

        /// <summary>
        /// Draw the power up block
        /// </summary>
        /// <param name="spriteBatch">the spritebatch object</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(spriteSheet, rec, textureRec, Color.White);
        }

    }
}
